package com.heima.article.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.es.ApArticleSearch;
import com.heima.article.es.ApArticleSearchRepository;
import com.heima.article.service.IApArticleService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: ApArticleEsInit
 * @time: 2022/7/10
 */
@SpringBootTest
public class ApArticleEsInit {

    @Autowired
    private IApArticleService apArticleService;

    @Autowired
    private ApArticleSearchRepository apArticleSearchRepository;

    @Test
    public void initES(){
        LambdaQueryWrapper<ApArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticle::getIsDelete,false);
        queryWrapper.eq(ApArticle::getIsDown,false);
        List<ApArticle> articleList = apArticleService.list(queryWrapper);
        List articleSearchList = new ArrayList();
        for (ApArticle apArticle : articleList) {
            ApArticleSearch apArticleSearch = new ApArticleSearch();
            BeanUtils.copyProperties(apArticle, apArticleSearch);
            articleSearchList.add(apArticleSearch);
        }
        apArticleSearchRepository.saveAll(articleSearchList);
    }
}
